#include <stdio.h>

void permutations(char tab[], int tab_len, int i) {
	if (i == tab_len-1)
		printf("%s\n", tab);
	else {
		int r = tab_len-i; //rounds
		while (r--) {
			int j;
			char e = tab[i]; //leftmost character
			//ROL operation
			for (j = i+1; j<tab_len; j++)
				tab[j-1] = tab[j]; //shift left
			tab[tab_len-1] = e; //restore
			//recursive call
			permutations(tab, tab_len, i+1);
		}
	}
}

int main(int argc, char* argv[])
{
	if (argc == 2) {
		char* s = argv[1];
		permutations(s, strlen(s)-1, 0);
		return 0;
	}
	else {
		printf("Usage: %s target\n", argv[0]);
		return 1;
	}
}
